package com.zhongying.customer;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zhongying.jf.constant.AccountTypeConstants;
import com.zhongying.jf.result.Result;
import com.zhongying.jf.service.AmountLogService;
import com.zhongying.sys.utils.PageUtils;
import com.zhongying.sys.utils.R;

/**
 * @author 周 浩
 * @email zhou_eric90@163.com
 * @date 2018年4月3日 下午1:54:02
 * @描述
 */
@Controller
@RequestMapping("/amount_log")
public class AmountLogController {

	@Autowired
	private AmountLogService amountLogService;

	/**
	 * 查看资金流水
	 * 
	 * @param startTime
	 *            交易开始时间
	 * @param endTime
	 *            交易结束时间
	 * @param trendType
	 *            交易类型
	 * @param userCode
	 *            用户ID
	 * @return
	 */
	@RequestMapping("/list_records")
	@ResponseBody
	public R listAmountLogs(String startTime, String endTime, String type, String userCode, Integer page, Integer limit) {
		if (StringUtils.isBlank(userCode) || page == null || page <= 0 || limit == null || limit <= 0) {
			return R.error(-1, "请求参数非法");
		}
		startTime = StringUtils.isBlank(startTime) ? null : startTime;
		endTime = StringUtils.isBlank(endTime) ? null : endTime;
		type = StringUtils.isBlank(type) ? "all" : type;
		List<String> types = null;
		switch (type) {
		case "all":// 全部
			types = null;
			break;
		case "others":
			types = AccountTypeConstants.ALL_TYPES;
			types.removeAll(AccountTypeConstants.SHOWED_TYPES);
			break;
		default:
			types = new ArrayList<>();
			String[] typeArr = type.split(",");
			for (String t : typeArr) {
				types.add(t);
			}
			break;
		}
		Result result = amountLogService.listAmountLogs(userCode, startTime, endTime, types, page, limit);
		Map<String, Object> model = (Map<String, Object>) result.getModel();
		Integer total = (Integer) model.get("total");
		List<Map<String, Object>> sourceList = (List<Map<String, Object>>) model.get("datas");
		sourceList = convertDatas(sourceList);
		Collections.sort(sourceList, new Comparator<Map<String, Object>>() {
			@Override
			public int compare(Map<String, Object> o1, Map<String, Object> o2) {
				return -((Date) o1.get("create_time")).compareTo((Date) o2.get("create_time"));
			}
		});
		PageUtils pageUtil = new PageUtils(sourceList, total, limit, page);
		return R.ok().put("page", pageUtil);
	}

	private List<Map<String, Object>> convertDatas(List<Map<String, Object>> sourceList) {
		if (!CollectionUtils.isEmpty(sourceList)) {
			for (Map<String, Object> source : sourceList) {
				// TODO
				source.put("status", "成功");
				Object typeObj = source.get("type");
				String type = (typeObj == null) ? "" : String.valueOf(typeObj);
				String trendType = AccountTypeConstants.ACCOUNT_TYPE_MAP.get(type);
				source.put("trend_type", trendType == null ? "" : trendType);
			}
		}
		return sourceList;
	}

}
